package ytl;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import ytl.*;

public class DBConnection{
    
    float Tax = (float) 0.12;
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "[ytl]";
    String driver = "com.mysql.jdbc.Driver";
    PreparedStatement DBQuery = null;
    ResultSet Result = null;
    String driverName = "org.gjt.mm.mysql.Driver";
    String serverName = "localhost";
    String url2 = "jdbc:mysql://" + serverName + "/" +db;
    String username = "root";
    String password = "";
    
    public void Init_DBC(){
        try{
        FileInputStream fstream = new FileInputStream("systems.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        
        br.readLine();
	url = br.readLine();
        driver = br.readLine();
	db = br.readLine();
        username = br.readLine();
	password = br.readLine();
        Tax = Float.parseFloat(String.format("%.2f", Float.parseFloat(br.readLine())));
	
        }catch (Exception ex){
        con = null;
        url = "jdbc:mysql://localhost:3306/";
        db = "[ytl]";
        driver = "com.mysql.jdbc.Driver";
        username = "root";
        password = "";
        DBQuery = null;
        Result = null;
        }
        
    }
    
    public void Init_DBC(String new_url, String new_driver, String new_db, String new_username, String new_password, String new_tax){
        url = new_url;
        db = new_db;
        driver = new_driver;
        username = new_username;
        password = new_password;
        Tax = Float.parseFloat(String.format("%.2f", Float.parseFloat(new_tax)));
    }
    
    public Connection Connect()
    {
        Connection conn = null;        
        try{
            try {
                Class.forName(driverName);
                conn = (Connection) DriverManager.getConnection(url2, username, password);
                Class.forName(driver);
                } catch (ClassNotFoundException e) {
                } catch (SQLException e) {
            }                   
        }
        finally
        {
            return conn;
        }
    }
    
    public ResultSet Query_Retrieve(String query) throws ClassNotFoundException, SQLException{
        Result = null;
        Class.forName(driver);
        con = DriverManager.getConnection(url+db, username, password);
        DBQuery = con.prepareStatement(query);
        Result = DBQuery.executeQuery();
        return Result;
    }
    
    public void Query_Send(String query) throws ClassNotFoundException, SQLException{
        Result = null;
        Class.forName(driver);
        con = DriverManager.getConnection(url+db, username, password);
        DBQuery = con.prepareStatement(query);
        DBQuery.executeUpdate(query);
    }
    
    public void DBClose(){
        try {
            con.close();
        } catch (SQLException ex) {}
    }
    
    public void main(String[] args) {
    
    }
}